package com.timetable;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.easylib.base.BaseActivity;
import com.easylib.views.DividerGridItemDecoration;
import com.guyj.CommonAdapter;
import com.guyj.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 课程表
 */
public class TabelActivity extends BaseActivity {

    @Bind(R.id.recycler)
    RecyclerView recycler;
    @Bind(R.id.activity_tabel)
    RelativeLayout activityTabel;
    GridLayoutManager glm;
    List<TableBean> beans;
    CommonAdapter adapter;
    private int spanCount=8;


    @Override
    public int getContentViewId() {
        return R.layout.activity_tabel;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

    }

    @Override
    protected void initVariable() {
        glm=new GridLayoutManager(mContext,spanCount);
        beans=new ArrayList<>();
        //第一行假数据
        beans.add(new TableBean("",""));
        beans.add(new TableBean("1","周一"));
        beans.add(new TableBean("2","周二"));
        beans.add(new TableBean("3","周三"));
        beans.add(new TableBean("4","周四"));
        beans.add(new TableBean("5","周五"));
        beans.add(new TableBean("6","周六"));
        beans.add(new TableBean("7","周日"));

        beans.add(new TableBean("6:00","-\n7:00"));
        beans.add(new TableBean("语文","教室A"));
        beans.add(new TableBean("数学","教室B"));
        beans.add(new TableBean("英语","教室C"));
        beans.add(new TableBean("体育","操场"));
        beans.add(new TableBean("马哲","教室D"));
        beans.add(new TableBean("无课",""));
        beans.add(new TableBean("无课",""));

        beans.add(new TableBean("8:00","-\n9:00"));
        beans.add(new TableBean("语文","教室A"));
        beans.add(new TableBean("数学","教室B"));
        beans.add(new TableBean("英语","教室C"));
        beans.add(new TableBean("体育","操场"));
        beans.add(new TableBean("马哲","教室D"));
        beans.add(new TableBean("无课",""));
        beans.add(new TableBean("无课",""));

        beans.add(new TableBean("10:00","-\n11:00"));
        beans.add(new TableBean("数学","教室B"));
        beans.add(new TableBean("语文","教室A"));
        beans.add(new TableBean("体育","操场"));
        beans.add(new TableBean("马哲","教室D"));
        beans.add(new TableBean("英语","教室C"));
        beans.add(new TableBean("无课",""));
        beans.add(new TableBean("无课",""));

        beans.add(new TableBean("12:00","-\n13:00"));
        beans.add(new TableBean("数学","教室B"));
        beans.add(new TableBean("语文","教室A"));
        beans.add(new TableBean("体育","操场"));
        beans.add(new TableBean("马哲","教室D"));
        beans.add(new TableBean("英语","教室C"));
        beans.add(new TableBean("无课",""));
        beans.add(new TableBean("无课",""));

        beans.add(new TableBean("14:00","-\n15:00"));
        beans.add(new TableBean("英语","教室C"));
        beans.add(new TableBean("马哲","教室D"));
        beans.add(new TableBean("体育","操场"));
        beans.add(new TableBean("语文","教室A"));
        beans.add(new TableBean("数学","教室B"));
        beans.add(new TableBean("无课",""));
        beans.add(new TableBean("无课",""));

        beans.add(new TableBean("16:00","-\n17:00"));
        beans.add(new TableBean("英语","教室C"));
        beans.add(new TableBean("马哲","教室D"));
        beans.add(new TableBean("体育","操场"));
        beans.add(new TableBean("语文","教室A"));
        beans.add(new TableBean("数学","教室B"));
        beans.add(new TableBean("无课",""));
        beans.add(new TableBean("无课",""));

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        recycler.setLayoutManager(glm);
        recycler.addItemDecoration(new DividerGridItemDecoration(mContext));
        recycler.setAdapter(adapter = new CommonAdapter<TableBean>(mContext, R.layout.item_table, beans) {
            @Override
            protected void convert(ViewHolder viewHolder, final TableBean o, int i) {
                viewHolder.setText(R.id.tv_title,o.getTitle());
                viewHolder.setText(R.id.tv_content,o.getContent());

                viewHolder.setOnClickListener(R.id.ll_root, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showCustomizeDialog(o);
                    }
                });
            }
        });


    }

    private void showCustomizeDialog(final TableBean o) {
        AlertDialog.Builder customizeDialog =
                new AlertDialog.Builder(mContext);
        final View dialogView = LayoutInflater.from(mContext)
                .inflate(R.layout.dialog_et, null);
        customizeDialog.setTitle("请输入");
        customizeDialog.setView(dialogView);
        customizeDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 获取EditView中的输入内容
                        EditText edit_title = (EditText) dialogView.findViewById(R.id.edit_title);
                        EditText edit_content = (EditText) dialogView.findViewById(R.id.edit_content);
                        o.setTitle(edit_title.getText().toString().trim());
                        o.setContent(edit_content.getText().toString().trim());
                        adapter.setDatas(beans);
                    }
                });
        customizeDialog.setNegativeButton("取消", null);
        customizeDialog.show();
    }


}
